package com.dream.keigezhushou.Activity.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dream.keigezhushou.Activity.Uiutils.UiUtils;
import com.dream.keigezhushou.Activity.adapter.NineGridAdapter;
import com.dream.keigezhushou.Activity.bean.CombodesInfo;
import com.dream.keigezhushou.Activity.bean.CommentInfo;
import com.dream.keigezhushou.Activity.bean.Image;
import com.dream.keigezhushou.Activity.bean.NearKtvDetilsInfo;
import com.dream.keigezhushou.Activity.bean.XcInfo;
import com.dream.keigezhushou.Activity.view.NineGridLayout;
import com.dream.keigezhushou.Activity.wigdt.GradationScrollView;
import com.dream.keigezhushou.Activity.wigdt.MyListView;
import com.dream.keigezhushou.R;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class EveryDetailActivity extends AppCompatActivity implements GradationScrollView.ScrollViewListener {


    @BindView(R.id.ktvname_every)
    TextView mKtvnameEvery;
    @BindView(R.id.iv_every)
    RelativeLayout mIvEvery;
    @BindView(R.id.fh_every)
    TextView mFhEvery;
    @BindView(R.id.price_every)
    TextView mPriceEvery;
    @BindView(R.id.saleprice_every)
    TextView mSalepriceEvery;
    @BindView(R.id.sy_every)
    Button mSyEvery;
    @BindView(R.id.ratingBar_every)
    RatingBar mRatingBarEvery;
    @BindView(R.id.pf_every)
    TextView mPfEvery;
    @BindView(R.id.common_every)
    TextView mCommonEvery;
    @BindView(R.id.ktv_name_every)
    TextView mKtvNameEvery;
    @BindView(R.id.location_every)
    TextView mLocationEvery;
    @BindView(R.id.phone_every)
    ImageView mPhoneEvery;
    @BindView(R.id.combolist_every)
    MyListView mCombolistEvery;
    @BindView(R.id.f_every)
    View mFEvery;
    @BindView(R.id.lv_xiaochi_every)
    MyListView mLvXiaochiEvery;
    @BindView(R.id.examine_every)
    TextView mExamineEvery;
    @BindView(R.id.next_every)
    ImageView mNextEvery;
    @BindView(R.id.tv_every)
    TextView mTvEvery;
    @BindView(R.id.line_every)
    View mLineEvery;
    @BindView(R.id.validity_every)
    TextView mValidityEvery;
    @BindView(R.id.time_every)
    TextView mTimeEvery;
    @BindView(R.id.subscribe_every)
    TextView mSubscribeEvery;
    @BindView(R.id.des_every)
    TextView mDesEvery;
    @BindView(R.id.info_every)
    TextView mInfoEvery;
    @BindView(R.id.infodes_every)
    TextView mInfodesEvery;
    @BindView(R.id.service_every)
    TextView mServiceEvery;
    @BindView(R.id.service_des1_every)
    TextView mServiceDes1Every;
    @BindView(R.id.service_des2_every)
    TextView mServiceDes2Every;
    @BindView(R.id.hint_every)
    TextView mHintEvery;
    @BindView(R.id.hint_des1_every)
    TextView mHintDes1Every;
    @BindView(R.id.hint_des2_every)
    TextView mHintDes2Every;
    @BindView(R.id.ratingBar_pf_every)
    RatingBar mRatingBarPfEvery;
    @BindView(R.id.grade_every)
    TextView mGradeEvery;
    @BindView(R.id.grade_des_every)
    TextView mGradeDesEvery;
    @BindView(R.id.appraise_every)
    TextView mAppraiseEvery;
    @BindView(R.id.lv_commonlist_every)
    MyListView mLvCommonlistEvery;
    @BindView(R.id.ktvlist_lv_every)
    MyListView mKtvlistLvEvery;
    @BindView(R.id.scrollview_every)
    GradationScrollView mScrollviewEvery;
    @BindView(R.id.addcar_every)
    Button mAddcarEvery;
    @BindView(R.id.stampede_every)
    Button mStampedeEvery;
    @BindView(R.id.back)
    ImageView mBack;
    @BindView(R.id.shoucang_every)
    ImageView mShoucangEvery;
    @BindView(R.id.share)
    ImageView mShare;
    @BindView(R.id.rl_every)
    RelativeLayout mRlEvery;
    private int imageHeight;

    private List<XcInfo> mList = new ArrayList<>();
    private ArrayList<NearKtvDetilsInfo> nearktvList = new ArrayList<>();

    private ArrayList<CombodesInfo> comboList = new ArrayList<>();
    private ArrayList<CommentInfo> commmonlist = new ArrayList<>();

    private List<List<Image>> imagesList = new ArrayList<>();
    private String[][] images = new String[][]{
            {"file:///android_asset/img2.jpg", "250", "250"}
            , {"file:///android_asset/img3.jpg", "250", "250"}
            , {"file:///android_asset/img4.jpg", "250", "250"}
            , {"file:///android_asset/img5.jpg", "250", "250"}
            , {"file:///android_asset/img6.jpg", "250", "250"}
            , {"file:///android_asset/img7.jpg", "250", "250"}
            , {"file:///android_asset/img8.jpg", "250", "250"}
    };


    private ArrayList<Image> mItemList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.TRANSPARENT);
        }
        setContentView(R.layout.activity_every_detail);
        ButterKnife.bind(this);
        initData();
        initListeners();


        mLvXiaochiEvery.setFocusable(false);
        mLvXiaochiEvery.setAdapter(new Myadapter());
        mKtvlistLvEvery.setFocusable(false);
        mKtvlistLvEvery.setAdapter(new AAdapter());
        mCombolistEvery.setFocusable(false);
        mCombolistEvery.setAdapter(new ComboAdapter());
        mLvCommonlistEvery.setFocusable(false);
        mLvCommonlistEvery.setAdapter(new CommentAdapter());
    }


    private void initData() {

        for (int i = 0; i < 4; i++) {
            CommentInfo info = new CommentInfo();
            info.setDes("环境不错，音响也可以，赠送小吃赞个先，经常 来这，性价比高");
            info.setName("好孩子");
            info.setArr(images);
            info.setIv(getResources().getDrawable(R.mipmap.dsdd));
            info.setPingfen("4");
            info.setCommon("商家回复:亲，很高兴您能来我店消费，谢 谢您对本店的支持与厚爱。期待您的下次光 临哦！");
            commmonlist.add(info);
        }

        for (int i = 0; i < 6; i++) {
            CombodesInfo info = new CombodesInfo();
            info.setName("开心包");
            info.setNumber("(2-4人)");
            info.setDes("6小时欢唱+小吃饮品");
            info.setNum("2张");
            comboList.add(info);

        }
        for (int i = 0; i < 6; i++) {
            XcInfo info = new XcInfo();
            info.setName("雪花啤酒");
            info.setNumber("14瓶");
            info.setTotalprice("256");
            mList.add(info);

        }
        for (int g = 0; g < 6; g++) {
            NearKtvDetilsInfo info = new NearKtvDetilsInfo();
            info.setCombo("［未央路］周一至周日时段3选1欢唱＋小吃饮品套餐");
            info.setSaleprice("794");
            info.setPrice("188");
            info.setDistance("500m");
            info.setName("乐唱好声音KTV");
            info.setIv(getResources().getDrawable(R.mipmap.iconiv));

            nearktvList.add(info);
        }

        for (int i = 0; i < 7; i++) {
            mItemList = new ArrayList<>();
            for (int j = 0; j <= i; j++) {
                mItemList.add(new Image(images[j][0], Integer.parseInt(images[j][1]), Integer.parseInt(images[j][2])));

            }
            imagesList.add(mItemList);
        }

    }

    private void initListeners() {

        ViewTreeObserver vto = mIvEvery.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {


            @Override
            public void onGlobalLayout() {
                mIvEvery.getViewTreeObserver().removeGlobalOnLayoutListener(
                        this);
                imageHeight = mIvEvery.getHeight();

                mScrollviewEvery.setScrollViewListener(EveryDetailActivity.this);
            }
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onScrollChanged(GradationScrollView scrollView, int x, int y, int oldx, int oldy) {


        Window window = getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

        if (y <= 0) {   //设置标题的背景颜色
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.TRANSPARENT);

            mRlEvery.setBackgroundColor(Color.argb((int) 0, 144, 151, 166));
        } else if (y > 0 && y <= imageHeight) { //滑动距离小于banner图的高度时，设置背景和字体颜色颜色透明度渐变
            float scale = (float) y / imageHeight;
            float alpha = (255 * scale);
            // mRl.setTextColor(Color.argb((int) alpha, 255, 255, 255));
            mRlEvery.setBackgroundColor(Color.argb((int) alpha, 144, 151, 166));
            window.setStatusBarColor(Color.argb((int) alpha, 144, 151, 166));
            window.setNavigationBarColor(Color.argb((int) alpha, 144, 151, 166));
        } else {    //滑动到banner下面设置普通颜色
            mRlEvery.setBackgroundColor(Color.argb((int) 255, 255, 255, 255));
            window.setStatusBarColor(Color.argb((int) 255, 255, 255, 255));
            window.setNavigationBarColor(Color.argb((int) 255, 255, 255, 255));
        }
    }


    @OnClick({R.id.examine_every, R.id.back})
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.examine_every:

                Intent intent = new Intent(this, KtvActivity.class);
                startActivity(intent);
                break;

            case R.id.back:
                finish();
                break;

        }
    }


    class Myadapter extends BaseAdapter {
        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public XcInfo getItem(int position) {
            return mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
           ViewHolder holder = null;
            if (convertView == null) {
                convertView = View.inflate(EveryDetailActivity.this, R.layout.item_xc, null);
                holder = new ViewHolder();
                holder.name = (TextView) convertView.findViewById(R.id.name);
                holder.number = (TextView) convertView.findViewById(R.id.number);
                holder.totalprice = (TextView) convertView.findViewById(R.id.totalprice);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            XcInfo item = getItem(position);
            holder.name.setText(item.getName());

            holder.number.setText(item.getNumber());
            holder.totalprice.setText("¥" + item.getTotalprice());
            return convertView;
        }
    }

    static class ViewHolder {
        TextView name;
        TextView number;
        TextView totalprice;
    }


    class AAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return nearktvList.size();
        }

        @Override
        public NearKtvDetilsInfo getItem(int position) {
            return nearktvList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
          KtvViewHolder holde = null;
            if (convertView == null) {
                convertView = UiUtils.inflateView(R.layout.item_merchant_ktvdetails);
                holde = new KtvViewHolder();
                holde.combo = (TextView) convertView.findViewById(R.id.combo);
                holde.name = (TextView) convertView.findViewById(R.id.ktv_name);
                holde.price = (TextView) convertView.findViewById(R.id.price);
                holde.saleprice = (TextView) convertView.findViewById(R.id.pricesale);
                holde.distance = (TextView) convertView.findViewById(R.id.distance);
                holde.iv = (ImageView) convertView.findViewById(R.id.iv_icon);

                convertView.setTag(holde);
            } else {
                holde = (KtvViewHolder) convertView.getTag();
            }

            NearKtvDetilsInfo info = getItem(position);
            holde.iv.setImageDrawable(info.getIv());
            holde.combo.setText(info.getCombo());
            holde.distance.setText(info.getDistance());
            holde.name.setText(info.getName());
            holde.price.setText(info.getPrice());
            holde.saleprice.setText(info.getSaleprice());


            return convertView;
        }
    }

    static class KtvViewHolder {
        ImageView iv;
        TextView name;
        TextView combo;
        TextView price;
        TextView saleprice;
        TextView distance;
    }


    class ComboAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return comboList.size();
        }

        @Override
        public CombodesInfo getItem(int position) {
            return comboList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
          Holder holder = null;
            if (convertView == null) {
                convertView = UiUtils.inflateView(R.layout.item_combo_teamdetails);
                holder = new Holder();
                holder.name = (TextView) convertView.findViewById(R.id.name);
                holder.number = (TextView) convertView.findViewById(R.id.number);
                holder.num = (TextView) convertView.findViewById(R.id.num);
                holder.des = (TextView) convertView.findViewById(R.id.detail);
                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }


            CombodesInfo item = getItem(position);

            holder.name.setText(item.getName());
            holder.number.setText(item.getNumber());
            holder.des.setText(item.getDes());
            holder.num.setText(item.getNum());

            return convertView;
        }
    }

    static class Holder {
        TextView name;
        TextView number;
        TextView des;
        TextView num;
    }

    class CommentAdapter extends BaseAdapter {


        @Override
        public int getCount() {
            return commmonlist.size();
        }

        @Override
        public CommentInfo getItem(int position) {
            return commmonlist.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {


           CommonViewHolder holder = null;
            if (convertView == null) {
                convertView = UiUtils.inflateView(R.layout.item_comment_teamdetails);
                holder = new CommonViewHolder();
                holder.common = (TextView) convertView.findViewById(R.id.sjhf);
                holder.iv = (ImageView) convertView.findViewById(R.id.icon);

                holder.name = (TextView) convertView.findViewById(R.id.title);

                holder.des = (TextView) convertView.findViewById(R.id.des);

                holder.nineLayout = (NineGridLayout) convertView.findViewById(R.id.nine_grid_layout);
                holder.pf = (RatingBar) convertView.findViewById(R.id.ratingBar);

                convertView.setTag(holder);

            } else {
                holder = (CommonViewHolder) convertView.getTag();
            }


            CommentInfo info = getItem(position);
            holder.common.setText(info.getCommon());
            holder.iv.setImageDrawable(info.getIv());
            holder.name.setText(info.getName());
            holder.des.setText(info.getDes());
            holder.pf.setRating(Float.parseFloat(info.getPingfen()));
            //   pf.setRating(Float.valueOf(info.getPingfen()));


            List<Image> itemList = imagesList.get(position);

            if (itemList.isEmpty()) {

                holder.nineLayout.setVisibility(View.GONE);
            } else {
                holder.nineLayout.setVisibility(View.VISIBLE);

                handlerOneImage(holder, itemList, getApplicationContext());
            }


            return convertView;
        }

        private void handlerOneImage(CommonViewHolder holder, List<Image> itemList, Context context) {
            holder.nineLayout.setAdapter(new Gameadapter(context, mItemList));
//            holder.nineLayout.setOnItemClickListerner(new NineGridLayout.OnItemClickListerner() {
//                @Override
//                public void onItemClick(View view, int position) {
//                    //do some thing
//
//                }
//            });
        }


    }

    static class CommonViewHolder {
        ImageView iv;
        TextView name;

        TextView des;
        NineGridLayout nineLayout;
        TextView common;
        RatingBar pf;
    }

    class Gameadapter extends NineGridAdapter {


        public Gameadapter(Context context, List list) {
            super(context, list);
        }

        @Override
        public int getCount() {
            return (list == null) ? 0 : list.size();
        }

        @Override
        public String getUrl(int positopn) {
            return getItem(positopn) == null ? null : ((Image) getItem(positopn)).getUrl();
        }

        @Override
        public Object getItem(int position) {
            return (list == null) ? null : list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int i, View view) {

            ImageView iv = null;
            if (view != null && view instanceof ImageView) {
                iv = (ImageView) view;
            } else {
                iv = new ImageView(context);
            }
            iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
            iv.setBackgroundColor(context.getResources().getColor((android.R.color.transparent)));
            String url = getUrl(i);
            Picasso.with(context).load(getUrl(i)).placeholder(new ColorDrawable(Color.parseColor("#f5f5f5"))).into(iv);
            if (!TextUtils.isEmpty(url)) {
                iv.setTag(url);
            }
            return iv;
        }
    }

}
